import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

// 二叉树 课程代码
public class BinaryTree {

    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //public TreeNode root;

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        //E.right = H;

        return A;
    }

    // 前序遍历
    // A B D E H C F G
    void preOrder(TreeNode root){
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    List<Character> tmp = new ArrayList<>();
    public List<Character> preorderTraversal(TreeNode root) {
        if (root == null) return tmp;
        tmp.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return tmp;
    }

    // 中序遍历
    // D B E H A F C G
    void inOrder(TreeNode root){
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public List<Character> inorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }

        List<Character> leftTree = inorderTraversal(root.left);
        ret.addAll(leftTree);

        ret.add(root.val);

        List<Character> rightTree = inorderTraversal(root.right);
        ret.addAll(rightTree);

        return ret;
    }

        // 后序遍历
    // D H E B F G C A
    void postOrder(TreeNode root){
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    // 获取树中节点的个数
    public static int nodeSize = 0;
    int size(TreeNode root){
        if (root == null) {
            return 0;
        }
        nodeSize++;
        size(root.left);
        size(root.right);

        return nodeSize;
    }

    int size2(TreeNode root){
        int count = 0;
        if (root == null) {
            return 0;
        }

        int leftCount = size2(root.left);
        int rightCount = size2(root.right);
        count = leftCount + rightCount + 1;

        return count;

//        if (root == null) {
//            return 0;
//        }
//
//        return size2(root.left) + size2(root.right) + 1;
    }

    // 获取叶子节点的个数
    public static int leafSize = 0;
    void getLeafNodeCount(TreeNode root){
        if (root == null){
            return;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        //return leafSize;
    }

    int getLeafNodeCount2(TreeNode root){
        int leafCount = 0;
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }

        leafCount = getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);

        return leafCount;
    }

    // 子问题思路-求叶子结点个数
    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root, int k){
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }

        return getKLevelNodeCount(root.left, k-1) + getKLevelNodeCount(root.right, k-1);
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if (root == null) {
            return 0;
        }

        //return getHeight(root.left) > getHeight(root.right) ? getHeight(root.left)+1 : getHeight(root.right)+1;
        // 最好记录左右树高度，不然返回会重复计算左右树的高度，时间复杂度变大
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return leftH > rightH ? leftH+1 : rightH+1;
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val){
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode node = find(root.left, val);
        if (node != null) {
            return node;
        }
        node = find(root.right, val);
        if (node != null) {
            return node;
        }
        return null;
    }

    //层序遍历
    void levelOrder(TreeNode root){
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode ret = queue.poll();
            System.out.print(ret.val + " ");

            if (ret.left != null) {
                queue.offer(ret.left);
            }
            if (ret.right != null) {
                queue.offer(ret.right);
            }
        }
    }

    List<List<Character>> levelOrder2(TreeNode root){
        List<List<Character>> ret = new ArrayList<>();
        if (root == null) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while (size != 0) {
                TreeNode tmp = queue.poll();
                list.add(tmp.val);
                size--;
                if (tmp.left != null) {
                    queue.offer(tmp.left);
                }
                if (tmp.right != null) {
                    queue.offer(tmp.right);
                }
            }
            ret.add(list);
        }
        return ret;
    }

    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        if (root == null) return false;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }

        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                return false;
            }
        }
        return true;
    }
}
